package com.msk.web.controller.wlhy;

import com.google.common.collect.Maps;
import com.msk.common.core.controller.BaseController;
import com.msk.common.core.domain.R;
import com.msk.common.core.domain.model.LoginUser;
import com.msk.common.utils.StringUtils;
import com.msk.framework.web.utils.TokenUtils;
import com.msk.sys.domain.Dict;
import com.msk.sys.utils.DictMskUtils;
import com.msk.wlhy.domain.HtDriverFile;
import com.msk.wlhy.domain.HtSupplier;
import com.msk.wlhy.domain.TransportCapacitySupplierRel;
import com.msk.wlhy.service.HtCarFilesService;
import com.msk.wlhy.service.HtDriverFileService;
import com.msk.wlhy.service.HtSupplierService;
import com.msk.wlhy.service.TransportCapacitySupplierRelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author llh
 * @date 2023/5/5
 * @description 所属承运商接口
 */
@Api(tags = "所属承运商接口")
@RestController
@RequestMapping("/api/transportCapacitySupplierRel")
public class TransportCapacitySupplierRelController extends BaseController {

    @Autowired
    private HtDriverFileService htDriverFileService;

    @Autowired
    private HtSupplierService htSupplierService;

    @Autowired
    private TransportCapacitySupplierRelService transportCapacitySupplierRelService;

    @Autowired
    private HtCarFilesService htCarFilesService;

    /**
     * @return com.msk.common.core.domain.R<?>
     * @author llh
     * @date 2023/5/5
     * @params
     * @description 我的承运商列表接口&承运商中我的运力
     */
    @ApiOperation("我的承运商列表接口&承运商中我的运力")
    @PostMapping("/transportCapacitySupplierRelByPage")
    public R<?> transportCapacitySupplierRelByPage(@RequestBody TransportCapacitySupplierRel transportCapacitySupplierRel) {
        LoginUser user = TokenUtils.getMpLoginUser();
        if ("admin".equals(user.getUsername())) {
        } else {
            if ("1".equalsIgnoreCase(user.getLoginObject())) {
                if (TokenUtils.getDriverLogin() == null) {
                    return R.appFail(400101, "用户未认证");
                }
                HtDriverFile df = htDriverFileService.getDriverByUserId(user.getLoginId());
                transportCapacitySupplierRel.setDriverId(df.getId());
            } else if ("3".equalsIgnoreCase(user.getLoginObject())) {
                if (TokenUtils.getSupplierLogin() == null || !TokenUtils.getSupplierLogin().getAuditStatus().equals("1")) {
                    return R.appFail(400101, "用户未认证");
                }
                HtSupplier hs = htSupplierService.getHtSupplierByUserId(user.getLoginId());
                transportCapacitySupplierRel.setSupplierId(hs.getId());
            }
        }
        transportCapacitySupplierRel.getParams().put("orderByStr", "a.create_date desc");
        startPageJson(transportCapacitySupplierRel.getParams(), "");
        List<Map<String, Object>> list = null;
        if ("1".equalsIgnoreCase(user.getLoginObject())) {
            list = transportCapacitySupplierRelService.findSupplierRelList(transportCapacitySupplierRel);
        } else if ("3".equalsIgnoreCase(user.getLoginObject())) {
            list = transportCapacitySupplierRelService.suppilerMyCapacity(transportCapacitySupplierRel);
        }
        List<Dict> carTypeDictList = DictMskUtils.getDictListNew("car_type");
        for (Map<String, Object> result : list) {
            String carType = DictMskUtils.getDictLabelNew(String.valueOf(result.get("carType")), carTypeDictList, "");
            result.put("carTypeAlias", carType);
        }
        return R.appOk(getDataTable(list));
    }

    /**
     * @return com.msk.common.core.domain.R<?>
     * @author llh
     * @date 2023/5/6
     * @description 承运商关联申请审核接口
     */
    @ApiOperation("承运商关联申请审核接口")
    @PostMapping("/audit")
    public R<?> audit(@RequestBody TransportCapacitySupplierRel transportCapacitySupplierRel) {
        Map<String, Object> result = Maps.newHashMap();
        try {
            TransportCapacitySupplierRel transportCapacitySupplierRel1 = transportCapacitySupplierRelService.get(transportCapacitySupplierRel.getId());
            if (transportCapacitySupplierRel1 != null) {
                if (transportCapacitySupplierRel1.getSupplierId().equals(transportCapacitySupplierRel.getSupplierId())) {
                    transportCapacitySupplierRel1.setBindStatus(transportCapacitySupplierRel.getBindStatus());
                    transportCapacitySupplierRel1.setBingTime(new Date());
                    transportCapacitySupplierRelService.save(transportCapacitySupplierRel1);
                    if ("1".equals(transportCapacitySupplierRel.getBindStatus())) {
                        result.put("flag", true);
                        result.put("message", "运力关联成功");
                    } else {
                        result.put("flag", true);
                        result.put("message", "运力拒绝成功");
                    }

                } else {
                    result.put("flag", true);
                    result.put("message", "运力关联失败");
                }

            } else {
                result.put("flag", true);
                result.put("message", "运力关联失败");
            }

        } catch (Exception e) {
            result.put("flag", false);
            result.put("message", e.toString());
        }
        return R.appOk(result);
    }
    
    /**
     * @author llh
     * @date 2023/7/17
     * @return com.msk.common.core.domain.R<?>
     * @description 司机解绑承运商      
     */
    @ApiOperation("司机解绑承运商")
    @PostMapping("/unbindingSupplier")
    public R<?> unbindingSupplier(@RequestBody TransportCapacitySupplierRel transportCapacitySupplierRel){
        String id = transportCapacitySupplierRel.getId();
        if(StringUtils.isEmpty(id)){
            return R.appFail(400101,"司机承运商关联ID#为空");
        }
        HtDriverFile driverFile = TokenUtils.getDriverLogin();
        if(!"0".equals(driverFile.getDriverStatus())){
            return R.appOk("当前司机状态非闲置");
        }
        transportCapacitySupplierRelService.delete(transportCapacitySupplierRel);
        return R.appOk("解绑成功");
    }

    /**
     * @return 返回值: R<?>
     * @author 作者: yangjy
     * @description 描述:添加运力申请
     * @date 时间: 2023/5/23 14:34
     */
    @ApiOperation(value = "添加运力申请")
    @RequestMapping(value = "/save")
    public R<?> save(@RequestBody TransportCapacitySupplierRel transportCapacitySupplierRel) {
        Map<String, Object> result = Maps.newHashMap();
        try {
            LoginUser u = TokenUtils.getMpLoginUser();
            HtSupplier supplier = htSupplierService.getHtSupplierByUserId(u.getLoginId());
            if (supplier.getId() != null) {
                transportCapacitySupplierRel.setSupplierId(supplier.getId());
            }
            //根据承运商ID和司机ID判断运力是否存在
            TransportCapacitySupplierRel transportCapacitySupplierRel1 = transportCapacitySupplierRelService.getTransportCapacitySupplierRel(transportCapacitySupplierRel);
            //'状态 0:申请中 1:已绑定/已关联 2:已拒绝',
            if (transportCapacitySupplierRel1 != null && transportCapacitySupplierRel1.getId() != null) {
                if ("0".equalsIgnoreCase(transportCapacitySupplierRel1.getBindStatus())) {
                    result.put("flag", true);
                    result.put("message", "运力申请中");
                } else if ("1".equalsIgnoreCase(transportCapacitySupplierRel1.getBindStatus())) {
                    result.put("flag", true);
                    result.put("message", "运力已绑定");
                } else if ("2".equalsIgnoreCase(transportCapacitySupplierRel1.getBindStatus())) {
                    transportCapacitySupplierRel1.setBindStatus("0");
                    transportCapacitySupplierRelService.save(transportCapacitySupplierRel1);
                    result.put("flag", true);
                    result.put("message", "运力申请中");
                }
            } else {
                transportCapacitySupplierRel.setBindStatus("0");
                //第一次添加
                String message = validateModel(transportCapacitySupplierRel);
                if (StringUtils.isNotBlank(message)) {
                    result.put("flag", false);
                    result.put("message", message);
                    return R.appOk(result);
                }
                transportCapacitySupplierRelService.save(transportCapacitySupplierRel);
                result.put("flag", true);
                result.put("message", "运力添加成功");
            }

        } catch (Exception e) {
            System.out.println(e);
            result.put("flag", false);
            result.put("message", e.toString());
        }
        return R.appOk(result);
    }

    /**
     * @return 返回值: R<?>
     * @author 作者: yangjy
     * @description 描述: 添加运力中的搜索
     * @date 时间: 2023/5/23 13:54
     */
    @ApiOperation(value = "添加运力中的搜索")
    @PostMapping(value = {"/transportCapacitySupplierReladdByPage"})
    public R<?> transportCapacitySupplierReladdByPage(@RequestBody TransportCapacitySupplierRel transportCapacitySupplierRel) {
        LoginUser loginUser = TokenUtils.getMpLoginUser();
        HtSupplier htSupplier = TokenUtils.getSupplierLogin();
        transportCapacitySupplierRel.setSupplierId(htSupplier.getId());
        if ("admin".equals(loginUser.getUsername())) {
        } else {
            transportCapacitySupplierRel.preInsert(loginUser);
        }
        transportCapacitySupplierRel.getParams().put("orderByStr", "ab.create_date desc");
        startPageJson(transportCapacitySupplierRel.getParams(), "");
        List<TransportCapacitySupplierRel> list = transportCapacitySupplierRelService.findListByPhone(transportCapacitySupplierRel);
        for (TransportCapacitySupplierRel car : list) {
            String dictValue = DictMskUtils.getDictLabel(car.getCarType(), "car_type", "");
            car.setCarTypeAlias(dictValue);
        }
        return R.appOk(getDataTable(list));
    }

    /**
     * @return 返回值: R<?>
     * @author 作者: yangjy
     * @description 描述:查看运力详情
     * @date 时间: 2023/5/26 14:38
     */
    @ApiOperation(value = "运力详情")
    @PostMapping(value = "/capacityDetails")
    public R<?> capacityDetails(@RequestBody Map<String, Object> map) {
        List<Map<String, Object>> result = transportCapacitySupplierRelService.findCarByDriverId(StringUtils.getString(map.get("driverId")));
        List<Dict> cartype = DictMskUtils.getDictListNew("car_type");
        List<Dict> vehicleenergytype = DictMskUtils.getDictListNew("vehicle_energy_type");
        for (Map<String, Object> rs : result) {
            String carType = StringUtils.getString(rs.get("carType"));
            String vehicleEnergyType = StringUtils.getString(rs.get("vehicleEnergyType"));
            String carTypeAlias = DictMskUtils.getDictLabelNew(carType, cartype, "");
            String vehicleEnergyTypeAlias = DictMskUtils.getDictLabelNew(vehicleEnergyType, vehicleenergytype, "");
            rs.put("carType", carTypeAlias);
            rs.put("vehicleEnergyType", vehicleEnergyTypeAlias);
        }
        return R.appOk(result);
    }
    @ApiOperation(value = "承运商注销运力解绑")
    @PostMapping(value = "/unbindCapacity")
    public R<?> unbindCapacity(TransportCapacitySupplierRel transportCapacitySupplierRel){
        transportCapacitySupplierRel.setSupplierId(TokenUtils.getSupplierLogin().getId());
        transportCapacitySupplierRel.setBindStatus("1");
        List<Map<String, Object>> list = transportCapacitySupplierRelService.suppilerMyCapacity(transportCapacitySupplierRel);
        for(Map<String, Object> ls : list){
            String id = String.valueOf(ls.get("id"));
            transportCapacitySupplierRel.setId(id);
            transportCapacitySupplierRelService.delete(transportCapacitySupplierRel);
        }
        return R.appOk("解绑成功");
    }
}